package com.atguigu;

import io.minio.*;
import io.minio.errors.*;
import io.minio.messages.Bucket;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class App {
    public static void main(String[] args) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {

        //这叫构造者模式，build模式
        //创建MinioClient对象，我们的操作都是通过这个核心对象进行的操作
        MinioClient minioClient = MinioClient.builder()
                //账号密码
                .credentials("minioadmin","minioadmin")
                //端点
                .endpoint("http://192.168.216.129:9000").build();
        //判断名称的桶存不存在
        boolean flag = minioClient.bucketExists(BucketExistsArgs.builder().bucket("b002").build());
//        3.如果不存在，则先创建 并先创建策略
        if (!flag){
            //创建桶
            minioClient.makeBucket(MakeBucketArgs.builder().bucket("b002").build());

            //指定策略
            String   policy = """
                    {
                         "Version": "2012-10-17",
                         "Statement": [
                             {
                                 "Effect": "Allow",
                                 "Principal": {
                                     "AWS": [
                                         "*"
                                     ]
                                 },
                                 "Action": [
                                     "s3:GetObject"
                                 ],
                                 "Resource": [
                                     "arn:aws:s3:::b002/*"
                                 ]
                             }
                         ]
                     }
                    """;
            minioClient.setBucketPolicy(SetBucketPolicyArgs.builder()
//                    桶的名称
                    .bucket("b002")
                    //桶的策略
                    .config(policy)
                    .build());
        }

        //上传图片
        minioClient.uploadObject(UploadObjectArgs.builder()
                                .bucket("b002")
                //图片名称
                .object("公寓-健身房.jpg")
                //图片路径
                .filename("D:\\iders\\workspace\\7.images\\公寓-健身房.jpg").build());

    }
}
